import { ethers } from "ethers";

const ALCHEMY_MAINNET_WSSURL = 'wss://eth-mainnet.g.alchemy.com/v2/mkM5-GVJCs00ghRsjT7aU';
const provider = new ethers.WebSocketProvider(ALCHEMY_MAINNET_WSSURL)


// provider.on("block", (blockNumber) =>{
//     console.log("新区块:", blockNumber)
// })

// provider.on("pending", async (txHash) => {
//     console.log("待处理交易:", txHash);
//     let tx = await provider.getTransaction(txHash)
//     console.log(tx)
//     console.log('------------------')
// })
// 限制速率
function throttle(fn, delay) {
    let timer;
    return function(){
        if(!timer) {
            fn.apply(this, arguments)
            timer = setTimeout(()=>{
                clearTimeout(timer)
                timer = null
            },delay)
        }
    }
}
provider.on("pending", throttle(async (txHash) => {
    console.log("待处理交易:", txHash);
    let tx = await provider.getTransaction(txHash)
    console.log(tx)
    console.log('------------------')
}, 1000))